﻿using System.Net;
using System.IO;
using System.Diagnostics;

namespace HelpersDotNet.Web
{
    public enum HttpPostMode
    {
        UrlEncoded,
        Multipart,
        Xml,
        Raw
    }

    public class HttpHelper
    {
        public HttpPostMode HttpPostMode { get; set; }

        /// <summary>
        /// Username used for authentication.
        /// </summary>
        public string UserName { get; set; }

        /// <summary>
        /// Password for authentication.
        /// </summary>
        public string Password { get; set; }

        /// <summary>
        /// Address of the proxy server to be used.
        /// </summary>
        public string ProxyAddress { get; set; }

        /// <summary>
        /// Timeout for Web request in seconds. Default is 30.
        /// </summary>
        public int Timeout { get; set; }

        /// <summary>
        /// If set true, will automatically track cookies.
        /// </summary>
        public bool IsCookiesEnabled { get; set; }

        private CookieCollection _cookies;
        public CookieCollection Cookies
        {
            get
            {
                if (_cookies == null)
                    _cookies = new CookieCollection();
                return _cookies;
            }
            set { _cookies = value; }
        }

        public HttpWebResponse HttpWebResponse { get; set; }

        public HttpWebRequest HttpWebRequest { get; set; }

        /// <summary>
        /// The buffer size used for sending and receiving information.
        /// </summary>
        public int BufferSize { get; set; }

        public string UserAget { get; set; }

        public MemoryStream _postStream;
        public BinaryWriter _postData;

        public HttpHelper()
        {
            this.HttpPostMode = HttpPostMode.UrlEncoded;
            this.Timeout = 30;
            
            this.UserName = "";
            this.Password = "";

            this.ProxyAddress = "";
            this.IsCookiesEnabled = false;

        }


    }

    public static partial class WebHelper
    {
        //public static byte[] PostData(byte[] byteArray, string url, string contentType)
        //{
        //    WebRequest request = WebRequest.Create(url);

        //    request.ContentType = contentType;
        //    request.Method = "POST";

        //    Stream dataStream = null;

        //    try
        //    {
        //        // send the post
        //        request.ContentLength = byteArray.Length;
        //        dataStream = request.GetRequestStream();
        //        dataStream.Write(byteArray, 0, byteArray.Length); // send it
        //    }
        //    catch (WebException)
        //    {
        //        Debug.WriteLine("HTTP Post: Request Error.");
        //    }

        //    try
        //    {
        //        // Get the webresponse.
        //        WebResponse response = request.GetResponse();

        //        if (response == null)
        //            return null;

        //        //return response.GetResponseStream().ToByteArray();
        //    }
        //    catch (WebException)
        //    {
        //        Debug.WriteLine("HTTP Post: Response Error.");
        //    }

        //    return null;
        //}

        ////public byte[] PostData(byte[] byteArray, string url, string contentType, int timeOut, ICredentials credentials)
        //{

        //    Stream dataStream = null;
        //    WebResponse response = null;

        //    try
        //    {
        //        // Create a webrequest.
        //        WebRequest request = WebRequest.Create(url);

        //        if (request != null)
        //        {
        //            request.Timeout = timeOut;
        //            request.ContentType = contentType;
        //            request.Credentials = credentials;
        //            request.co

        //            request.ContentLength = byteArray.Length;
        //            request.Method = "POST";

        //            using (Stream stream = request.GetRequestStream())
        //            {
        //                stream.Write(byteArray, 0, byteArray.Length);
        //            }

        //            response = request.GetResponse();
        //            dataStream = response.GetResponseStream();

        //        }
        //    }
        //    catch
        //    {
        //        return null;
        //    }
        //    finally
        //    {
        //        if (response != null) response.Close();
        //        if (dataStream != null) dataStream.Close();
        //    }

        //    return dataStream.ToByteArray();
        //}

        //public byte[] PostData(byte[] byteArray, string url, string contentType)
        //{
        //    return PostData(byteArray, url, contentType, 1000, CredentialCache.DefaultCredentials);
        //}
    }
}